<script lang="ts" setup>
import {ref} from "vue"
import {ElMessage} from "element-plus"
import {upP} from "../serve.ts";
import {useRouter} from 'vue-router'
const
    router = useRouter(),
    loading = ref(false),
    fromRules = {
      oldPwd: [
        {
          required: true,
          min: 5,
          max: 15,
          message: '密码长度5-15位',
          trigger: 'trigger',
        },
      ],
      newPwd: [
        {
          required: true,
          min: 5,
          max: 15,
          message: '密码长度5-15位',
          trigger: 'trigger',
        },
      ],
      confirmPwd: [
        {
          required: true,
          min: 5,
          max: 15,
          message: '密码长度5-15位',
          trigger: 'trigger',
        },
      ]
    },
    froms = ref(),
    fromData = ref({
      "oldPwd": "",
      "newPwd": "",
      "confirmPwd": ""
    }),
    userName = ref(JSON.parse(sessionStorage.getItem('userInfoSy'))['username'])
;

async function change() {
  try {
    await froms.value.validate()
    if (fromData.value.newPwd != fromData.value.confirmPwd) {
      ElMessage({message: '新密码不一致', type: 'error',duration:1500})
      return
    }
    loading.value = true
    upP({...fromData.value}).then(()=>{
      ElMessage({message: '修改成功！', type: 'success',duration:1500})
      setTimeout(()=>{router.push('/login')},1000)
      sessionStorage.clear()
    })
  } catch (e) {
  }
}
</script>

<template>
  <el-scrollbar>
    <el-card v-loading="loading" box element-loading-text="加载中...">
      <template #header>修改密码</template>
      <el-form ref="froms" :model="fromData" :rules="fromRules" el-From>
        <el-form-item label="账号：" prop="userName">{{userName}}</el-form-item>
        <el-form-item label="原密码：" prop="oldPwd">
          <el-input v-model="fromData['oldPwd']" placeholder="请输入原密码" show-password style="width: 250px;"
                    type="password"/>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPwd">
          <el-input v-model="fromData['newPwd']" placeholder="请输入新密码" show-password style="width: 250px;"
                    type="password"/>
        </el-form-item>
        <el-form-item label="确认密码：" prop="confirmPwd">
          <el-input v-model="fromData['confirmPwd']" placeholder="请再次输入新密码" show-password style="width: 250px;"
                    type="password"/>
        </el-form-item>
        <el-form-item>
          <el-button round style="width: 65%;margin: auto;" type="success" @click="change">确定修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-scrollbar>
</template>

<style lang="scss" scoped>
[box] {
  width: 35%;
  margin: 30px auto auto;
}

[el-From] {
  margin: auto;
}
</style>